package cn.gov.pbc.dream.service.impl;

import java.awt.geom.Area;
import java.util.Date;

import cn.gov.pbc.dream.common.ResponseCode;
import cn.gov.pbc.dream.common.ServerResponse;
import cn.gov.pbc.dream.dao.AbroadDoMapper;
import cn.gov.pbc.dream.dao.CityDoMapper;
import cn.gov.pbc.dream.dao.DistrictDoMapper;
import cn.gov.pbc.dream.dao.ProvinceDoMapper;
import cn.gov.pbc.dream.pojo.bo.AreaBo;
import cn.gov.pbc.dream.pojo.query.bankfinancing.area.AreaQuery;
import cn.gov.pbc.dream.pojo.repository.AbroadDo;
import cn.gov.pbc.dream.pojo.repository.CityDo;
import cn.gov.pbc.dream.pojo.repository.DistrictDo;
import cn.gov.pbc.dream.pojo.repository.ProvinceDo;
import cn.gov.pbc.dream.pojo.vo.bankfinancing.area.AreaVo;
import cn.gov.pbc.dream.pojo.vo.bankfinancing.area.CityVo;
import cn.gov.pbc.dream.pojo.vo.bankfinancing.area.DistrictVo;
import cn.gov.pbc.dream.pojo.vo.bankfinancing.area.ProvinceVo;
import cn.gov.pbc.dream.service.IAreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.plugin2.os.windows.SECURITY_ATTRIBUTES;

import java.util.ArrayList;
import java.util.List;

@Service("areaService")
@Slf4j
public class AreaServiceImpl implements IAreaService {

    @Autowired
    private ProvinceDoMapper provinceDoMapper;

    @Autowired
    private CityDoMapper cityDoMapper;

    @Autowired
    private DistrictDoMapper districtDoMapper;

    @Autowired
    private AbroadDoMapper abroadDoMapper;

    @Override
    public ServerResponse getAndSaveAreaCode(String areaCode) {
        if (StringUtils.isBlank(areaCode) || areaCode.length() != 6) {
            return ServerResponse.createByError();
        }
        String sign = areaCode.substring(0, 3);
        if (sign.equals("000")) {
            //表示该区域在境外
            AbroadDo abroadDo = this.abroadDoMapper.selectByCode(areaCode);
            if (abroadDo == null) {
                abroadDo = new AbroadDo();
                abroadDo.setCode(areaCode);
                this.abroadDoMapper.insertSelective(abroadDo);
            }
            return ServerResponse.createBySuccess(abroadDo);
        } else {
            String provinceCode = areaCode.substring(0, 2);
            log.info(areaCode);
            if (provinceCode.equals("00")) {
                log.info(provinceCode);
            }
            ProvinceDo provinceDo = this.provinceDoMapper.selectByCode(provinceCode);
            if (provinceDo == null) {
                provinceDo = new ProvinceDo();
                provinceDo.setCode(provinceCode);
                int insertCount = this.provinceDoMapper.insertSelective(provinceDo);
                if (insertCount == 0) {
                    return ServerResponse.createByErrorMessage("写入province表出错");
                }
            }
            String cityCode = areaCode.substring(2, 4);
            CityDo cityDo = this.cityDoMapper.selectByCodeAndProvinceId(cityCode, provinceDo.getId());
            if (cityDo == null) {
                cityDo = new CityDo();
                cityDo.setCode(cityCode);
                cityDo.setProvinceId(provinceDo.getId());
                int insertCount = this.cityDoMapper.insertSelective(cityDo);
                if (insertCount == 0) {
                    return ServerResponse.createByErrorMessage("写入city表出错");
                }
            }
            String districtCode = areaCode.substring(4, 6);
            DistrictDo districtDo = this.districtDoMapper.selectByCodeAndCityId(districtCode, cityDo.getId());
            if (districtDo == null) {
                districtDo = new DistrictDo();
                districtDo.setCode(districtCode);
                districtDo.setCityId(cityDo.getId());
                int insertCount = this.districtDoMapper.insertSelective(districtDo);
                if (insertCount == 0) {
                    return ServerResponse.createByErrorMessage("写入 district表出错");
                }
            }
            return ServerResponse.createBySuccess(districtDo);
        }
    }

    @Override
    public ServerResponse getAllProvinceList() {
        List<ProvinceDo> provinceDoList = this.provinceDoMapper.selectAllProvince();
        List<ProvinceVo> provinceVoList = new ArrayList<>();

        for (ProvinceDo provinceDo : provinceDoList) {
            ProvinceVo vo = new ProvinceVo();
            vo.setId(provinceDo.getId());
            vo.setCode(provinceDo.getCode());
            vo.setName(provinceDo.getName());
            provinceVoList.add(vo);
        }
        return ServerResponse.createBySuccess(provinceVoList);
    }

    @Override
    public ServerResponse searchArea(AreaQuery areaQuery) {
        if (areaQuery == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        PageHelper.startPage(areaQuery.getPageNum(), areaQuery.getPageSize());
        if (StringUtils.isBlank(areaQuery.getSearchWord())) {
            areaQuery.setSearchWord(null);
        } else {
            areaQuery.setSearchWord("%" + areaQuery.getSearchWord() + "%");
        }
        if (areaQuery.getShowNoName() == null) {
            areaQuery.setShowNoName(Boolean.FALSE);
        }
        if (areaQuery.getDistrictId() == null || areaQuery.getDistrictId().equals(0)) {
            areaQuery.setDistrictId(null);
        }
        if (areaQuery.getCityId() == null || areaQuery.getCityId().equals(0)) {
            areaQuery.setCityId(null);
            areaQuery.setDistrictId(null);
        }
        if (areaQuery.getProvinceId() == null || areaQuery.getProvinceId().equals(0)) {
            areaQuery.setProvinceId(null);
            areaQuery.setCityId(null);
            areaQuery.setDistrictId(null);
        }
        List<DistrictDo> districtDoList = this.districtDoMapper.searchAllAreaByParam(
                areaQuery.getSearchWord(),
                areaQuery.getShowNoName(),
                areaQuery.getProvinceId(),
                areaQuery.getCityId(),
                areaQuery.getDistrictId()
        );
        List<AreaVo> areaVoList = new ArrayList<>();
        for (DistrictDo districtDo : districtDoList) {
            areaVoList.add(this.assembleAreaVo(districtDo));
        }
        PageInfo pageInfo = new PageInfo(districtDoList);
        pageInfo.setList(areaVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    private AreaVo assembleAreaVo(DistrictDo districtDo) {
        AreaVo areaVo = new AreaVo();
        areaVo.setDistrictId(districtDo.getId());
        if (!districtDo.getCode().equals("00")) {
            areaVo.setDistrictName(districtDo.getName());
        }
        CityDo cityDo = this.cityDoMapper.selectByPrimaryKey(districtDo.getCityId());
        if (!cityDo.getCode().equals("00")) {
            areaVo.setCityName(cityDo.getName());
        }
        ProvinceDo provinceDo = this.provinceDoMapper.selectByPrimaryKey(cityDo.getProvinceId());
        areaVo.setProvinceName(provinceDo.getName());
        areaVo.setAreaCode(provinceDo.getCode() + cityDo.getCode() + districtDo.getCode());
        return areaVo;
    }

    @Override
    public ServerResponse getCityByProvinceId(Integer provinceId) {
        if (provinceId == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<CityDo> cityDoList = this.cityDoMapper.selectByProvinceId(provinceId);
        List<CityVo> cityVoList = new ArrayList<>();

        for (CityDo cityDo : cityDoList) {
            CityVo vo = new CityVo();
            vo.setId(cityDo.getId());
            vo.setCode(cityDo.getCode());
            vo.setName(cityDo.getName());
            cityVoList.add(vo);
        }
        return ServerResponse.createBySuccess(cityVoList);
    }

    @Override
    public ServerResponse getDistrictByCityId(Integer cityId) {
        if (cityId == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<DistrictDo> districtDoList = this.districtDoMapper.selectByCityId(cityId);
        List<DistrictVo> districtVoList = new ArrayList<>();

        for (DistrictDo districtDo : districtDoList) {
            DistrictVo vo = new DistrictVo();
            vo.setId(districtDo.getId());
            vo.setCode(districtDo.getCode());
            vo.setName(districtDo.getName());
            districtVoList.add(vo);
        }
        return ServerResponse.createBySuccess(districtVoList);
    }

    @Override
    public ServerResponse getFullAreaByDistrictId(Integer districtId, Integer abroadId) {
        if (districtId == null && abroadId == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        AreaBo bo = new AreaBo();

        if (districtId == null) {
            AbroadDo abroadDo = this.abroadDoMapper.selectByPrimaryKey(abroadId);
            if(abroadDo == null){
                return ServerResponse.createByErrorMessage("无信息");
            }
            bo.setCode(abroadDo.getCode());
            bo.setName(abroadDo.getName());
            return ServerResponse.createBySuccess(bo);
        }
        DistrictDo districtDo = this.districtDoMapper.selectByPrimaryKey(districtId);
        if(districtDo == null){
            return ServerResponse.createByErrorMessage("获取区县信息失败");
        }
        CityDo cityDo = this.cityDoMapper.selectByPrimaryKey(districtDo.getCityId());
        if(cityDo == null){
            return ServerResponse.createByErrorMessage("获取城市信息失败");
        }
        ProvinceDo provinceDo = this.provinceDoMapper.selectByPrimaryKey(cityDo.getProvinceId());
        if(provinceDo == null){
            return ServerResponse.createByErrorMessage("获取省份信息失败");
        }
        bo.setCode(provinceDo.getCode() + cityDo.getCode() + districtDo.getCode());
        bo.setProvinceName(provinceDo.getName());
        bo.setCityName(cityDo.getName());
        bo.setDistrictName(districtDo.getName());
        StringBuffer sb = new StringBuffer();
        if(StringUtils.isNotBlank(provinceDo.getName()) && !provinceDo.getCode().equals("00")){
            sb.append(provinceDo.getName());
        }
        if(StringUtils.isNotBlank(cityDo.getName()) && !cityDo.getCode().equals("00")){
            sb.append(cityDo.getName());
        }
        if(StringUtils.isNotBlank(districtDo.getName()) && !districtDo.getCode().equals("00")){
            sb.append(districtDo.getName());
        }
        if(StringUtils.isNotBlank(sb)){
            bo.setName(sb.toString());
        }
        return ServerResponse.createBySuccess(bo);
    }
}
